import { defineStore } from 'pinia';
import { userApi } from '@/api';
import type { UserInfo } from '@/api/types';

export const useUserStore = defineStore('user', {
  state: () => ({
    userInfo: null as UserInfo | null,
    token: localStorage.getItem('token')
  }),
  
  actions: {
    async login(username: string, password: string) {
      const { data } = await userApi.login({ username, password });
      this.userInfo = data.data;
      // 实际项目中这里应该设置token
      localStorage.setItem('token', 'mock-token');
      this.token = 'mock-token';
    },

    async getUserInfo() {
      const { data } = await userApi.getUserInfo();
      this.userInfo = data.data;
    },

    logout() {
      this.userInfo = null;
      this.token = null;
      localStorage.removeItem('token');
    }
  }
});
